﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AutoMapper;

namespace AutoMapperSamples.Samples.ListAndArrayMaping
{
    /// <summary>
    /// 集合和数组映射测试类
    /// </summary>
    [TestClass]
    public class ListAndArrayMaping
    {
        [TestMethod]
        public void ListMapingTest()
        {
            //初始化映射 和单个对象的映射一样
            Mapper.Initialize(cfg => cfg.CreateMap<Source, Destination>());

            var srcList = new List<Source> {
                new Source { Value = 5,Text="Five" }                
            };

            // 在这里指定类型参数；源类型：List<Source>；目标类型： IEnumerable<Destination>；
            IEnumerable<Destination> ienumerableDest1 = Mapper.Map<List<Source>, IEnumerable<Destination>>(srcList);
            ICollection<Destination> icollectionDest1 = Mapper.Map<List<Source>, ICollection<Destination>>(srcList);
            IList<Destination> ilistDest1 = Mapper.Map<List<Source>, IList<Destination>>(srcList);
            List<Destination> listDest1 = Mapper.Map<List<Source>, List<Destination>>(srcList);
            Destination[] arrayDest1 = Mapper.Map<List<Source>, Destination[]>(srcList);

            //验证结果
            foreach (var m in ienumerableDest1)
            {
                Assert.AreEqual("Five", m.Text);//通过
                Assert.AreEqual(5, m.Value); //通过
            }
            //验证结果
            foreach (var m in listDest1)
            {
                Assert.AreEqual("Five", m.Text); //通过
                Assert.AreEqual(5, m.Value); //通过
            }

        }
    }

    /// <summary>
    /// 源对象
    /// </summary>
    public class Source
    {
        public int Value { get; set; }
        public string Text { get; set; }
    }

    /// <summary>
    /// 目标对象
    /// </summary>
    public class Destination
    {
        public int Value { get; set; }
        public string Text { get; set; }
    }
}
